﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using TecBaseObjects.Objects;

namespace TecData.Layer {

    public class TimerFileDataLayer : BaseSqlDataLayer {

        #region Members

        private const string loadTimerFileStatement = @"[timer].GetTimerFile";
        private const string loadTimerFileByUserStatement = @"[timer].GetTimerFileByUser";
        private const string saveTimerFileStatement = @"[timer].SaveTimerFile";
        private const string removeTimerFileStatement = @"[timer].RemoveTimerFile";

        #endregion

        #region Load

        public virtual TimerFileObj LoadTimerFile(string fileName, int userID) {
            string sql = loadTimerFileStatement;
            using (IDataReader reader = this.GetDataReaderFromStoredProcedure(sql, new SqlParameter("@FileName", fileName), new SqlParameter("@UserID", userID))) {
                return (reader.Read() ? LoadTimerFileFromDataReader(reader) : null);
            }
        }

        public virtual List<TimerFileObj> LoadTimerFilesByUser(int userID) {
            string sql = loadTimerFileByUserStatement;
            List<TimerFileObj> timerFileList = new List<TimerFileObj>();
            using (DataTable dt = this.GetDataTableFromStoredProcedure(sql, new SqlParameter("@UserID", userID))) {
                foreach (DataRow dr in dt.Rows) {
                    TimerFileObj tf = new TimerFileObj();
                    tf.FileName =(string)dr["FileName"];
                    tf.SetDate = (DateTime)dr["SetDate"];
                    tf.TimerCount = (int)dr["TimerCount"];
                    tf.UserID = (int)dr["UserID"];
                    timerFileList.Add(tf);
                }
            }
            return timerFileList;
        }

        #endregion

        #region Save

        public virtual void RemoveTimerFile(string fileName, int userID) {
            try {
                int ret = ExecuteStoredProcedure(removeTimerFileStatement, new SqlParameter("@FileName", fileName), new SqlParameter("@UserID", userID));
            } finally {
                //toDo: log in file
            }
        }

        public virtual void SaveTimerFile(TimerFileObj tf) {
            SqlParameter[] parameters = GetParameters(tf);
            ExecuteStoredProcedure(saveTimerFileStatement, parameters);
        }

        #endregion

        #region Functions

        protected virtual SqlParameter[] GetParameters(TimerFileObj tf) {
            SqlParameter[] ps = new SqlParameter[4];

            ps[0] = new SqlParameter("@FileName", tf.FileName);
            ps[1] = new SqlParameter("@SetDate", tf.SetDate);
            ps[2] = new SqlParameter("@TimerCount", tf.TimerCount);
            ps[3] = new SqlParameter("@UserID", tf.UserID);

            return ps;

        }

        protected virtual TimerFileObj LoadTimerFileFromDataReader(IDataReader reader) {
            TimerFileObj tf = new TimerFileObj();
            tf.FileName = this.GetString(reader, "FileName");
            tf.SetDate = this.GetDateTime(reader, "SetDate");
            tf.TimerCount = this.GetInt32(reader, "TimerCount");
            tf.UserID = this.GetInt32(reader, "UserID");
            return tf;
        }

        #endregion

    }
}
